package studentsinfomanagement.dao;

import studentsinfomanagement.bean.Administrator;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 管理员数据层类
 *
 */
public class AdministratorDAO extends BaseDAO{

    private static AdministratorDAO administratorDAO = null;

    /**
     * 单例模式
     */
    private AdministratorDAO(){}

    /**
     * 获取单例对象
     * @return 单例对象
     */
    public static synchronized AdministratorDAO getInstance(){
        if(administratorDAO == null){
            administratorDAO = new AdministratorDAO();
        }
        return administratorDAO;
    }

    /**
     * 根据账号查询管理员是否存在
     * @param accNum 账号
     * @return 查询结果
     */
    private int queryByAccNum(String accNum){
        String sql = "select * from administrator where accNum=?";
        String[] param = {accNum};
        ResultSet rs = db.executeQuery(sql,param);
        int result = 0;
        try{
            if (rs.next()) {//查询到管理员
                result = 1;
            }
        } catch (SQLException e) {
            result = 0;
        }finally {
            destroy();//关闭resultSet
        }
        return result;
    }

    /**
     * 根据账号获取管理员信息
     * @param accNum 账号
     * @return 查询到的对象
     */
    public Administrator getAdministratorByAccNum(String accNum){
        String sql = "select * from administrator where accNum=?";
        String[] param = {accNum};
        ResultSet rs = db.executeQuery(sql,param);
        Administrator result = null;
        try{
            if (rs.next()) {//查询到管理员
                result = new Administrator();
                result.setAccNum(accNum);
                result.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            result = null;
        }finally {
            destroy();//关闭resultSet
        }
        return result;
    }

    /**
     * 根据账号和密码验证登录信息
     * @param accNum 是账号
     * @param password 密码
     * @return 登录结果
     */
    public boolean queryForLogin(String accNum, String password){
        boolean result = false;
        if (accNum.length() == 0 || password.length() == 0) {
            return result;
        }
        //编写sql语句，准备参数
        String sql = "select * from administrator where accNum=?";
        String[] param = { accNum };
        //调用数据库工具的查询方法
        rs = db.executeQuery(sql, param);
        //查询得到结果则验证通过
        try {
            if (rs.next()) {
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            destroy();
        }
        return result;
    }

    /**
     * 更新管理员密码
     * @param administrator 管理员对象
     * @return 更新结果
     */
    public boolean update(Administrator administrator) {
        boolean result = false;
        if (administrator == null) {
            return result;
        }
        // 查询管理员是否存在
        if (queryByAccNum(administrator.getAccNum()) == 0) {
            return result;
        }
        // 执行更新操作
        String sql = "update administrator set password=? where accNum=?";
        String[] param = { administrator.getPassword(),administrator.getAccNum() };
        int rowCount = db.executeUpdate(sql, param);
        if (rowCount == 1) {
            result = true;
        }
        destroy();
        return result;
    }

}
